package com.dfbz.netctoss.utils;

import com.dfbz.netctoss.entity.AdminInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;

public class ShiroUtils {

    public static Subject getSubjct(){
        return SecurityUtils.getSubject();
    }

    public static AdminInfo getAdmin(){
        return (AdminInfo) getSubjct().getPrincipal();
    }

    /**
     * 动态更新
     * @param adminInfo
     */
    public static void setAdmin(AdminInfo adminInfo) {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principalCollection = subject.getPrincipals();
        String realmName = principalCollection.getRealmNames().iterator().next();
        PrincipalCollection newPrincipalCollection =
                new SimplePrincipalCollection(adminInfo, realmName);
        subject.runAs(newPrincipalCollection);
    }
}
